package com.zzw.controller;

import com.zzw.entity.OutStore;
import com.zzw.entity.Result;
import com.zzw.entity.Store;
import com.zzw.page.Page;
import com.zzw.service.OutStoreService;
import com.zzw.service.StoreService;
import com.zzw.entity.CurrentUser;
import com.zzw.utils.TokenUtils;
import com.zzw.utils.WarehouseConstants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Project: warehouse
 * @Description:
 * @Author: zzw
 */
@RestController
@RequestMapping("/outStore")
@Slf4j
@Api(tags = "出库模块")
public class OutStoreController {

    @Autowired
    private OutStoreService outStoreService;

    @Autowired
    private TokenUtils tokenUtils;

    @Autowired
    private StoreService storeService;

    /**
     * 查询所有仓库的url接口
     */
    @GetMapping("/store-list")
    public Result storeList() {
        //执行业务
        List<Store> storeList = storeService.queryAllStore();
        //响应
        return Result.ok(storeList);
    }

    /**
     * 添加出库单的url接口
     *
     * @param outStore
     * @param token
     * @return
     * @RequestBody OutStore outStore将添加的出库单信息的json数据封装到参数OutStore对象;
     * @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token
     * 将请求头Token的值即客户端归还的token赋值给参数变量token;
     */
    @PostMapping
    @ApiOperation("添加出库单")
    public Result saveOutStore(@RequestBody OutStore outStore, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token) {

        //获取当前登录用户
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        //获取当前登录用户的id,即添加出库单的用户id
        int createBy = currentUser.getUserId();
        outStore.setCreateBy(createBy);

        //添加出库单
        return outStoreService.saveOutStore(outStore);
    }

    /**
     * 分页查询出库单的url接口
     *
     * @param page
     * @param outStore
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询出库单")
    public Result outStorePageList(Page page, OutStore outStore) {
        Page page1 = outStoreService.outStorePage(page, outStore);
        return Result.ok(page1);
    }

    /**
     * 确定出库的url接口
     *
     * @param outStore
     * @return
     */
    @PostMapping("/confirm")
    @ApiOperation("确认出库单")
    public Result confirmOutStore(@RequestBody OutStore outStore) {
        return outStoreService.confirmOutStore(outStore);
    }

    /**
     * 导出数据
     *
     * @param page
     * @param outStore
     * @return
     */
    @GetMapping("/exportTable")
    public Result exportTable(Page page, OutStore outStore) {
        page = outStoreService.outStorePage(page, outStore);
        List<?> resultList = page.getResultList();
        //响应
        return Result.ok(resultList);
    }
}
